<template>
  <div class="dialog-container">
    <div class="dialog-content">
      <!--搜索-->
      <div class="panel-container">
        <SearchUiNew ref="SearchUiNew" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading">
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="公告名称" prop="title" fixed="left" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="申请用户名" prop="apply_user_name" width="150"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="申请电话" prop="apply_user_phone" width="150"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="补充说明" prop="desc" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="是否通话" prop="is_contact">
            <template slot-scope="scope">
              {{ scope.row.is_contact ? '是' : '否' }}
            </template>
          </af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="回访人" prop="contact_user_name"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="备注" prop="remark" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="申请时间" prop="created_time" width="150"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="操作" fixed="right" width="80">
            <template slot-scope="scope">
              <el-button type="text" @click="handleFollowUp(scope)">回访</el-button>
            </template>
          </af-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
      </div>
    </div>

    <!--  回访 -->
    <el-dialog title="回访" :visible.sync="showDialogFollowUp" :append-to-body="true">
      <DialogFollowUp v-if="showDialogFollowUp" @close="showDialogFollowUp = false" :id="currentId" @success="handleSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { apiNoticeApplyList } from '@/api/biddingNotice'
import DialogFollowUp from './DialogFollowUp.vue'

export default {
  name: 'DialogSignUp',
  components: { DialogFollowUp },
  props: {
    id: String,
    time: Object,
  },
  data() {
    return {
      showDialogFollowUp: false,
      searchFormArr: [
        {
          fieldName: '信息搜索:',
          type: 'input',
          field: 'search',
          placeholder: '请输入信息搜索',
        },
        {
          fieldName: '',
          type: 'select',
          field: 'search_type',
          labelWidth: '0px',
          default: 'title',
          clearable: false,
          option: [
            { value: 'title', label: '公告名称' },
            { value: 'notice_id', label: '公告UUID' },
            { value: 'apply_user_name', label: '申请用户名' },
            { value: 'apply_user_phone', label: '申请电话' },
            { value: 'contact_user_name', label: '操作人' },
          ],
          placeholder: '请选择',
        },
        {
          fieldName: '精准搜索',
          type: 'checkbox',
          field: 'is_precise',
          placeholder: '请选择',
        },
        {
          fieldName: '申请时间:',
          type: 'timePicker',
          field: 'time',
          placeholder: '请选择申请时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
      ],
      searchParams: {
        title: '',
        notice_id: '',
        apply_user_name: '',
        apply_user_phone: '',
        contact_user_name: '',
        is_precise: '',
        start_time: '',
        end_time: '',
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      currentId: '',
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (this.id) {
        this.searchParams.notice_id = this.id
      }
      if (this.time) {
        const { start_time, end_time } = this.time
        this.searchParams.start_time = start_time
        this.searchParams.end_time = end_time
      }

      this.$nextTick(() => {
        const setItemList = []
        if (this.id) {
          setItemList.push({ key: 'search', value: this.id })
          setItemList.push({ key: 'search_type', value: 'notice_id' })
        }
        if (this.time) {
          const { start_time, end_time } = this.time
          setItemList.push({ key: 'time', value: [start_time, end_time] })
        }
        if (setItemList.length > 0) {
          this.$refs.SearchUiNew.setItemValue(setItemList)
        } else {
          this.getDataList()
        }
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageNumber, pageSize: this.pageSize }, this.searchParams)
      apiNoticeApplyList(info)
        .then((res) => {
          const { data, total } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['is_precise', 'search', 'search_type', 'time']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      if (e.is_precise) {
        this.searchParams.is_precise = 1
      } else {
        this.searchParams.is_precise = 0
      }
      const formItem1 = this.searchFormArr.find((item) => item.field === 'search_type')
      const option1 = formItem1.option
      option1.forEach((item) => {
        this.searchParams[item.value] = ''
      })
      if (e.search) {
        this.searchParams[e.search_type] = e.search
      }
      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    handleFollowUp(scope) {
      this.currentId = scope.row.id
      this.showDialogFollowUp = true
    },
    handleSuccess() {
      this.showDialogFollowUp = false
      this.getDataList()
    },
  },
}
</script>

<style scoped lang="scss">
.panel-line {
  margin-bottom: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
